﻿using UnityEngine;
using System.Collections.Generic;


public class UIRecordData
{
    public int viewId;
    public object customDta;
}

/// <summary>
/// 维护管理UI层级，所有UI的显示关闭，都需要通过此类
/// </summary>
public class GameUi:MonoBehaviour
{

    public static GameUi Instance;

    /// <summary>
    /// 管理UI显示事件
    /// </summary>
    public System.Action<UiView> OnShowViewEvent;
    /// <summary>
    /// UI关闭
    /// </summary>
    public System.Action<UiView> OnRemoveViewEvent;

    public Camera uiCamera;

    private List<UiLayer> layerList = new List<UiLayer>();
    /// <summary>
    /// Ui回退
    /// </summary>
    private Stack<UIRecordData> uIRecordDataStack = new Stack<UIRecordData>();

    #region unity函数
    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {

    }

    private void OnDestroy()
    {

    }
    #endregion

    #region 自定义函数


    /// <summary>
    /// 获取图层
    /// </summary>
    /// <param name="layerType">图层</param>
    /// <returns></returns>
    public UiLayer GetLayer(int layerType)
    {
        for (int i = 0; i < layerList.Count; i++)
        {
            if (layerList[i] != null && layerList[i].layerType == layerType)
            {
                return layerList[i];
            }
        }
        return null;
    }

    /// <summary>
    /// 获取或加图层
    /// </summary>
    /// <param name="layerType"></param>
    /// <returns></returns>
    public UiLayer GetAddLayer(int layerType)
    {
        UiLayer uiLayer = GetLayer(layerType);
        if (uiLayer == null)
        {
            uiLayer = new UiLayer();
            uiLayer.layerType = layerType;
            layerList.Add(uiLayer);
        }
        return uiLayer;
    }

    /// <summary>
    /// 将Ui添加到图层，
    /// </summary>
    /// <param name="uiView"></param>
    public void AddView(UiView uiView)
    {
        if (uiView != null)
        {
            UiLayer uiLayer = GetAddLayer(uiView.LayerType);
            if (uiLayer != null)
            {
                uiLayer.AddView(uiView);
            }
        }
    }

    /// <summary>
    /// 显示Ui
    /// </summary>
    /// <param name="viewId"></param>
    /// <returns></returns>
    public UiView ShowView(int viewId)
    {
        UiConfig uiConfig = UiConfigManger.GetConfig(viewId);
        if (uiConfig != null && !string.IsNullOrEmpty(uiConfig.pathFileName))
        {
            //查看是否有冲突层
            for (int i = 0; i < uiConfig.closeLayerList.Count; i++)
            {
                //查看是否有图层冲突的UI
                //关闭图层
                OptimizeCloseLayer(uiConfig.pathFileName[i]);
            }
            //获取UI
            UiView uiView = GetView(uiConfig);
            if (uiView==null)
            {
                Object assetObject = Resources.Load(uiConfig.pathFileName);
                if (assetObject != null)
                {
                    GameObject uiGameObject = Instantiate(assetObject) as GameObject;
                    if (uiGameObject != null)
                    {
                        uiGameObject.transform.SetParent(transform, false);
                        UnityTools.ResetTransform(uiGameObject.transform);
                        Canvas canvas = uiGameObject.GetComponent<Canvas>();
                        canvas.worldCamera = uiCamera;

                        uiView = uiGameObject.GetComponent<UiView>();
                        if (uiView != null)
                        {
                            uiView.SetUIConfig(uiConfig);
                            uiView.OnLoad();

                            AddView(uiView);

                            uiView.SetAcitve(true);
                            
                        }
                        else
                        {
                            Debug.Log(viewId + " Needs a UIView Script!!!");
                        }
                    }
                }
                else
                {
                    Debug.Log(uiConfig.pathFileName + " can't be loaded");
                }
            }
            else
            {
                uiView.SetAcitve(true);
                return uiView;

            }

            if (OnShowViewEvent != null && uiView != null)
            {
                OnShowViewEvent(uiView);
            }

            return uiView;
        }
        return null;
    }

 
        /// <summary>
        /// 根据配置获取View
        /// </summary>
        /// <param name="uiConfig"></param>
        /// <returns></returns>
     public UiView GetView(UiConfig uiConfig)
    {
        if (uiConfig!=null)
        {
            UiLayer uiLayer = GetAddLayer(uiConfig.layerType);
            if (uiLayer!=null)
            {
                UiView uiView = uiLayer.GetView(uiConfig.viewId);
                if (uiView!=null)
                {
                    return uiView;
                }
            }
        }
        return null;
    }

    /// <summary>
    /// 根据id编号获取view
    /// </summary>
    /// <param name="viewId"></param>
    /// <returns></returns>
    public UiView GetView(int viewId)
    {
        UiConfig uiConfig = UiConfigManger.GetConfig(viewId);
        UiView uiView = GetView(uiConfig);

        return uiView;
    }

    /// <summary>
    /// 根据id删除view
    /// </summary>
    /// <param name="viewId"></param>
    public void RemoveView(int viewId)
    {
        UiConfig uiConfig = UiConfigManger.GetConfig(viewId);
        if (uiConfig!=null)
        {
            UiLayer uiLayer = GetAddLayer(uiConfig.layerType);
            if (uiLayer!=null)
            {
                uiLayer.RemoveView(viewId);
            }
        }
    }

    /// <summary>
    /// 删除view
    /// </summary>
    /// <param name="uiView"></param>
    public void RemoveView(UiView uiView)
    {
        if (uiView!=null)
        {
            UiLayer uiLayer = GetAddLayer(uiView.LayerType);
            if (uiLayer != null)
            {
                uiLayer.RemoveView(uiView);
                Destroy(uiView.gameObject);
            }

            //if (OnRemoveViewEvent!=null)
            //{
            //    OnRemoveViewEvent(uiView);
            //}


            OnRemoveViewEvent?.Invoke(uiView);
        }
    }

    /// <summary>
    /// 判断有无View
    /// </summary>
    /// <param name="viewId"></param>
    /// <returns></returns>
    public bool HasView(int viewId)
    {
        UiConfig uiConfig = UiConfigManger.GetConfig(viewId);
        if (uiConfig!=null)
        {
            UiLayer uiLayer = GetAddLayer(uiConfig.layerType);
            if (uiLayer!=null&&uiLayer.HasView(viewId))
            {
                return true;
            }
        }

        return false;
    }

    /// <summary>
    /// 启用或关闭图层
    /// </summary>
    /// <param name="layerType"></param>
    /// <param name="active"></param>
    public void SetLayerActive(int layerType,bool active)
    {
        UiLayer uiLayer = GetAddLayer(layerType);
        if (uiLayer!=null)
        {
            uiLayer.SetLayerActive(active);
        }
    }

    /// <summary>
    /// 关闭当前层所有UI
    /// </summary>
    /// <param name="layerType"></param>
    public void ClearLayer(int layerType)
    {
        UiLayer uiLayer = GetAddLayer(layerType);
        if (uiLayer != null)
        {
            uiLayer.ClearLayer();
        }
    }

    /// <summary>
    /// 隐藏大UI关闭其余小UI
    /// </summary>
    /// <param name="layerType"></param>
    public void OptimizeCloseLayer(int layerType)
    {
        UiLayer uiLayer = GetAddLayer(layerType);
        if (uiLayer!=null)
        {
            uiLayer.OptimizeCloseLayer();
        }
    }

    /// <summary>
    /// 关闭所有层
    /// </summary>
    public void CloseAllLayer()
    {
        for (int i = 0; i < layerList.Count; i++)
        {
            layerList[i].ClearLayer();
        }
    }
    
    public void ClearRecordData()
    {
        uIRecordDataStack.Clear();
    }

    /// <summary>
    /// 做ui回退用
    /// </summary>
    /// <param name="viewId"></param>
    /// <param name="customData"></param>
    public void SetRecorData(int viewId, object customData = null)
    {
        ClearRecordData();

        UIRecordData uIRecordData = new UIRecordData();
        uIRecordData.viewId = viewId;
        uIRecordData.customDta = customData;

        uIRecordDataStack.Push(uIRecordData);
    }

    public void PushRecordData(int viewId,object customData)
    {
        UIRecordData uIRecordData = new UIRecordData();
        uIRecordData.viewId = viewId;
        uIRecordData.customDta = customData;

        uIRecordDataStack.Push(uIRecordData);
    }

    public UIRecordData PopRecordData()
    {
        //if (uIRecordDataStack.Count>0)
        //{
        //    return uIRecordDataStack.Pop();
        //}
        //return null;
        return uIRecordDataStack.Count > 0 ? uIRecordDataStack.Pop() : null;
    }

    #endregion

}

